/*
 * File: gov.kdrm.mygset.util.GridUtil.java
 *
 * Copyright (c) 2009 PT. XYBASE INDONESIA,
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of PT. XYBASE INDONESIA ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with PT. XYBASE INDONESIA.
 *
 * Project:	mygset2
 * Created: Jun 17, 2009 10:32:00 AM
 */
package app.rci.util;


import java.sql.Date;
import java.sql.Time;
import java.util.List;
import java.util.ArrayList;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import app.rci.config.CommonConstants;

/**
 * <p>This class is a grid wrapper to be used with the xytable.js component.</p>
 * <p>This class is extending from JSONUtil so it supports json output by serializing this class</p>
 *
 * <p>Revision Information:<br/><i>
 * $Date: 2009-11-20 17:48:03 +0700 (Fri, 20 Nov 2009) $<br/>
 * $Revision: 10480 $
 * $LastChangedBy$
 * </i></p>
 *
 * @author Imam
 */
public class GridUtil extends JSONUtil {
    /**
     * Current page, default to 1
     */
    private int page;
    /**
     * Maximum rows to display, default to CommonConstants.ROWS_PER_PAGE
     */
    private int rows;
    /**
     * The actual result list
     */
    private List<?> resultList;
    /**
     * Total records available
     */
    private int totalRecords;
    /**
     * Current offset, starts from 0
     */
    private int offset;
    /**
     * Last user updated the result list
     */
    private String lastUpdateBy;
    /**
     * last Date this result list is updated
     */
    private Date lastUpdateDate;
    /**
     * Last Time this result list is updated
     */
    private Time lastUpdateTime;
    
    private List<?> originalList;

    public GridUtil() {
        super();
    }

    public int getPage() {
        page = Math.round((offset/getRows()) + 0.5f);
        return page > getTotalPages() ? getTotalPages() : page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        rows = rows != 0 ? rows : CommonConstants.ROWS_PER_PAGE;
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getTotalPages() {
        return Math.round((totalRecords/getRows()) + 0.5f);
    }

    public int getOffset() {
        return offset < 0 ? 0 : offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public List<?> getResultList() {
        return resultList;
    }

    public void setResultList(List<?> resultList) {
        this.resultList = resultList;
    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    /**
	 * @return the lastUpdateBy
	 */
	public String getLastUpdateBy() {
		return lastUpdateBy;
	}

	/**
	 * @param lastUpdateBy the lastUpdateBy to set
	 */
	public void setLastUpdateBy(String lastUpdateBy) {
		this.lastUpdateBy = lastUpdateBy;
	}

	/**
	 * @return the lastUpdateDate
	 */
	public String getLastUpdateDate() {
		return lastUpdateDate != null ? DateTimeUtil.formatDateToStr(lastUpdateDate, CommonConstants.DATE_FORMAT) : "xx/xx/xxxx";
	}

	/**
	 * @param lastUpdateDate the lastUpdateDate to set
	 */
	public void setLastUpdateDate(Date lastUpdateDate) {
		this.lastUpdateDate = lastUpdateDate;
	}

	/**
	 * @return the lastUpdateTime
	 */
	public String getLastUpdateTime() {
		return lastUpdateTime != null ? DateTimeUtil.formatTimeToStr(lastUpdateTime, CommonConstants.TIME_FORMAT_SHORT) : "00:00";
	}

	/**
	 * @param lastUpdateTime the lastUpdateTime to set
	 */
	public void setLastUpdateTime(Time lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}

    /**
     * Method to cut list based on offset and maximum rows to display
     *
     * @return paged list based on offset and maximum rows to display
     */
    public List<?> getPagedResultList() {
        int max = getTotalRecords() - getOffset();
        List<Object> pagedList = new ArrayList<Object>();
        for (int i = getOffset(); (i - getOffset()) < (max > getRows() ? getRows() : max); i++)
            pagedList.add(resultList.get(i));
        return pagedList;
    }

    /**
     * Serialize this class into json format
     *
     * @return json string represented the grid 
     */
	@Override
    public String serialize() {
        addData("rows", getResultList());
        addData("page", getPage());
        addData("records", getTotalRecords());
        addData("total", getTotalPages());
        return super.serialize();
    }

	@Override
	public String toString(){
		return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
	}

	public List<?> getOriginalList() {
		return originalList;
	}

	public void setOriginalList(List<?> originalList) {
		this.originalList = originalList;
	}
}
